package mymac.com.test_3_1;

import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONObject;


/**
 * Created by mymac on 2017/2/27.
 */

public class MyHomeFragment extends Fragment implements View.OnClickListener{
    private TextView tv_userName;
    private Button btn_switchUser,btn_logout,btn_version;
    private SharedPreferences preferences;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_home,null);
        preferences = getActivity().getSharedPreferences("token", Activity.MODE_PRIVATE);
        tv_userName = (TextView) view.findViewById(R.id.tv_userName);
        btn_switchUser = (Button) view.findViewById(R.id.btn_switchUser);
        btn_logout = (Button) view.findViewById(R.id.btn_logout);
        btn_version = (Button) view.findViewById(R.id.btn_version);
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        String userName = preferences.getString("userName","");
        tv_userName.setText(userName);
        setOnClick();
    }

    private void setOnClick() {
        btn_switchUser.setOnClickListener(this);
        btn_logout.setOnClickListener(this);
        btn_version.setOnClickListener(this);
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_switchUser:
                switchUser();
                break;
            case R.id.btn_logout:
                logout();
                break;
            case R.id.btn_version:
                version(view);
                break;
        }
    }

    public void switchUser() {
        System.out.println("切换用户");

    }

    public void logout(){
        System.out.println("注销");
        String token = preferences.getString("token","");
        final String address = "http://a.xl18z.cn/api/logout?token=" + token;
        HttpActivity.getInstance().getJson(address, new DataCallBack() {
            @Override
            public void onErr(Object object) {
                JSONObject json = (JSONObject)object;
                String errcode = json.optString("errcode");
                if ("0".equals(errcode)){
                    Toast.makeText(getActivity(), "注销成功", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(getActivity(),LoginActivity.class);
                    startActivity(intent);
                    getActivity().finish();
                } else {
                    Toast.makeText(getActivity(), "注销失败", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    public void version(View view){
        final View popupWindow_version = getActivity().getLayoutInflater().inflate(R.layout.activity_popversion,null);
        final PopupWindow popupWindow = new PopupWindow(popupWindow_version, ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setBackgroundDrawable(new ColorDrawable(0));
        popupWindow.setOutsideTouchable(true);
        final TextView tv_version = (TextView) popupWindow_version.findViewById(R.id.tv_version);
        System.out.println("版本信息");
        String token = preferences.getString("token","");
        final String address = "http://a.xl18z.cn/api/version?token=" + token;
        HttpActivity.getInstance().getJson(address, new DataCallBack() {
            @Override
            public void onErr(Object object) {
                JSONObject json = (JSONObject)object;
                JSONObject record = json.optJSONObject("record");
                String version = record.optString("version");
                tv_version.setText(version);
            }
        });
        popupWindow.showAtLocation(view, Gravity.CENTER,0,0);
    }

    /**
     * 获取版本号
     * @return 当前应用的版本号
     */
    private String getVersionName() throws Exception{
        //获取packagemanager的实例
        PackageManager packageManager = getActivity().getPackageManager();
        //getPackageName()是你当前类的包名，0代表是获取版本信息
        PackageInfo packInfo = packageManager.getPackageInfo(getActivity().getPackageName(), 0);
        return packInfo.versionName;
    }


}
